如何从命令行使用Apt-Get在Ubuntu中安装程序

如何从命令行使用Apt-Get在Ubuntu中安装程序

Ubuntu has a lot of GUI-based methods for installing applications, but they take some time to search and find. Since the keyboard is usually faster than the mouse, managing your software via the command-line can be a real time-saver.

Ubuntu有很多基于GUI的方法来安装应用程序,但是它们需要一些时间来搜索和查找。 由于键盘通常比鼠标快,因此通过命令行管理软件可以节省大量时间。

易于 (APT)

Linux manages software through packages, individual units of software that contain user interfaces, modules, and libraries. Most applications link several co-dependent packages together, and still others allow you to choose which packages to install and which to leave out at your own discretion. This can get confusing, so there’s a package manager at your disposal to help

Linux通过软件包,包含用户界面,模块和库的单个软件单元来管理软件。 大多数应用程序将几个相互依赖的软件包链接在一起,还有一些应用程序允许您自行选择安装哪些软件包,以及忽略哪些软件包。 这可能会造成混乱,因此您可以使用包管理器来提供帮助

Each Linux distribution has its own package management system. For our own near and dear Ubuntu, it’s the Advanced Packaging Tool. It has a family of commands that allows you to add repositories; search for, install, and remove packages; and even simulate upgrades and such. The commands are fairly easy to remember and use, so you’ll be managing your system’s software in no time at all!

每个Linux发行版都有其自己的软件包管理系统。 对于我们自己亲爱的Ubuntu,它是高级打包工具。 它具有一系列命令,可用于添加存储库。 搜索,安装和删除软件包; 甚至模拟升级等。 这些命令非常容易记住和使用,因此您将立即管理系统的软件!

APT requires super-user permissions, as it deals with core aspects of the system, so in Ubuntu you’ll need to preface most commands with “sudo.”

APT需要超级用户权限,因为它涉及系统的核心方面,因此在Ubuntu中,您需要在大多数命令前加上“ sudo”。

搜索包 (Searching for Packages)

The command to search for software is:

搜索软件的命令是:

apt-cache search [search term 1] [search term 2] … [search term n]

apt-cache搜索[搜索词1] [搜索词2]…[搜索词n]

Replace [search terms] but don’t use brackets. You’ll get an output like this:

替换[搜索词],但不要使用方括号。 您将获得如下输出:

如何从命令行使用Apt-Get在Ubuntu中安装程序

You can search for terms in the description of packages, say for a solitaire game, or by package name. Some searches may yield a ton of results, so you can scroll through the list with the following command:

您可以在包装说明中搜索术语,例如单人纸牌游戏或按包装名称搜索。 某些搜索可能会产生大量结果,因此您可以使用以下命令滚动浏览列表:

apt-cache search [search terms] | less

apt-cache搜索[搜索词] | 减

There’s a pipe in the middle of that command (it shares a key with \). The less command will allow you to scroll through your list with the arrow keys, page up/down keys, and space, b, and enter. Hit q to exit the list and go back to the prompt.

该命令中间有一个管道(它与\共享一个**)。 less命令将允许您使用箭头键,上一页/下一页键和空格b滚动浏览列表,然后输入。 按q退出列表并返回提示。

添加存储库 (Adding Repositories)

You can find more software in repositories found online. Take, for instance, Ubuntu Tweak, a program that lets you change some hidden or otherwise difficult-to-change settings for your system. It’s hosted at another repository. If you add the repository instead of downloading and installing just the package, the system will notify you of updates and automatically keep it up-to-date for you. You can manually add and change repositories by editing APT’s sources file:

您可以在网上找到的存储库中找到更多软件。 以Ubuntu Tweak为例,该程序可让您更改系统的某些隐藏设置或其他难以更改的设置。 它托管在另一个存储库中。 如果您添加存储库而不是仅下载和安装软件包,系统将通知您更新并自动为您更新。 您可以通过编辑APT的源文件来手动添加和更改存储库:

sudo nano /etc/apt/sources.list

须藤nano /etc/apt/sources.list

But Ubuntu 9.10 Karmic Koala changed that. There’s an easier way!

但是Ubuntu 9.10 Karmic Koala改变了这一点。 有一种更简单的方法!

如何从命令行使用Apt-Get在Ubuntu中安装程序

sudo add-apt-repository [repository name here]

sudo add-apt-repository [这里的存储库名称]

Let’s look at Ubuntu Tweak’s repo to see what it’ll look like in practice:

让我们看一下Ubuntu Tweak的仓库,看看它在实践中会是什么样子:

sudo add-apt-repository ppa:tualatrix/ppa

sudo add-apt-repository ppa:tualatrix / ppa

Voila!

瞧!

更新来源 (Updating Sources)

如何从命令行使用Apt-Get在Ubuntu中安装程序

After adding repositories, you have to update your package list.

添加存储库后,您必须更新软件包列表。

sudo apt-get update

sudo apt-get更新

That will update the package lists from all repositories in one go. Remember to do this after every added repository!

这将一次性更新所有存储库中的软件包列表。 请记住在每个添加的存储库之后执行此操作!

安装 (Installation)

Now that you’ve added your software repo and updated your package list, and found the package name you need, you can install it.

现在,您已经添加了软件仓库并更新了软件包列表,并找到了所需的软件包名称,接下来就可以安装它了。

sudo apt-get install [package name 1] [package name 2] … [package name n]

sudo apt-get install [软件包名称1] [软件包名称2]…[软件包名称n]

如何从命令行使用Apt-Get在Ubuntu中安装程序

This will download and install all of the packages listed. If there are dependencies – other prerequisite packages – they will also be installed. Sometimes you’ll also see a list of recommended but optional packages to go along with your selection. Sometimes, you’ll also see a confirmation prompt, though not always.

这将下载并安装所有列出的软件包。 如果存在依赖关系-其他必备软件包-也将安装它们。 有时,您还会看到一系列推荐但可选的软件包以及您选择的软件包。 有时,您也会看到确认提示,尽管并非总是如此。

Often, you’ll see a core package with other linked packages, so installing this one will automatically install the dependencies and sometimes its associated packages, too.

通常,您会看到一个核心程序包以及其他链接的程序包,因此安装该程序包将自动安装依赖项,有时还会自动安装相关的程序包。

如何从命令行使用Apt-Get在Ubuntu中安装程序

移除包裹 (Removing Packages)

If you want to get rid of a program, you can uninstall its associated packages.

如果要删除程序,则可以卸载其关联的程序包。

sudo apt-get remove [package name 1] [package name 2] … [package name n]

sudo apt-get remove [软件包名称1] [软件包名称2]…[软件包名称n]

如何从命令行使用Apt-Get在Ubuntu中安装程序

If you want to get rid of the configuration files and associated directories (usually in the user’s home directory), you’ll want to add the purge option:

如果要删除配置文件和关联的目录(通常在用户的主目录中),则需要添加清除选项:

sudo apt-get remove –purge [package name 1] [package name 2] … [package name n]

sudo apt-get remove –purge [软件包名称1] [软件包名称2]…[软件包名称n]

There are two dashes there. This will come in handy if a program isn’t working properly. By purging upon removal, you’ll can have a “clean” install.

那里有两个破折号。 如果程序无法正常运行,这将派上用场。 通过清除后清除,您可以进行“全新”安装。

Most of the time, you can just choose the core package and the associated ones will be removed as well. If it doesn’t, you can use the following command:

在大多数情况下,您只需选择核心软件包,相关的软件包也将被删除。 如果没有,则可以使用以下命令:

sudo apt-get autoremove

须藤apt-get autoremove

This will automatically remove any packages that aren’t used or associated with any installed program. For example, if you got rid of a core package, autoremove will get rid of it’s associated packages and any dependencies it had, so long as no other program is using them. It’s a great way to clean up any unused libraries and packages you don’t need.

这将自动删除所有未使用或与任何已安装程序无关的软件包。 例如,如果您放弃了一个核心软件包,则只​​要没有其他程序正在使用它们,autoremove就会摆脱它的关联软件包以及它所具有的任何依赖关系。 这是清理不需要的未使用库和包的好方法。

升级软件 (Upgrading Software)

So, what if your packages need upgrading? You can upgrade individual programs with the following command:

那么,如果您的软件包需要升级怎么办? 您可以使用以下命令升级单个程序:

sudo apt-get upgrade [package name 1] [package name 2] … [package name n]

sudo apt-get upgrade [软件包名称1] [软件包名称2]…[软件包名称n]

Or, you can upgrade all packages by having no further arguments:

或者,您可以通过没有其他参数来升级所有软件包:

sudo apt-get upgrade

sudo apt-get升级

This will tell you how many and which packages need updating and will ask for a confirmation before it continues.

这将告诉您有多少包以及哪些包需要更新,并在继续之前要求确认。

Remember, you may need to update first. Upgrade will replace older versions of programs with their newer versions. This is a replacement process; the same package name is required and the older version is replaced with a newer version. No completely new packages are installed and no packages are uninstalled.

请记住,您可能需要先进行更新。 升级会将程序的旧版本替换为新版本。 这是一个替换过程; 必须使用相同的软件包名称,并且旧版本将替换为新版本。 没有安装全新的软件包,也没有卸载软件包。

Some programs don’t quite work that way. They require a package with a slightly different name to be removed and a new one with a different name to be installed. Sometimes a program’s new version has a new required package. In these cases, you’ll need to use dist-upgrade.

某些程序不是那样工作的。 他们需要删除名称稍有不同的软件包,并安装名称不同的新软件包。 有时程序的新版本具有新的必需程序包。 在这些情况下,您将需要使用dist-upgrade。

如何从命令行使用Apt-Get在Ubuntu中安装程序

sudo apt-get dist-upgrade [package name 1] [package name 2] … [package name n]

sudo apt-get dist-upgrade [软件包名称1] [软件包名称2]…[软件包名称n]

sudo apt-get dist-upgrade

须藤apt-get dist-upgrade

Now, all of the dependencies will be satisfied no matter what. If you aren’t into micro-managing your packages, then this is the command you’re going to use.

现在,无论如何,所有依赖关系都将得到满足。 如果您不打算对软件包进行微管理,那么这就是您将要使用的命令。

If you only want to see which packages will be upgraded if you were to hypothetically run the command, you can simulate an upgrade with the –s option.

如果只想查看假设要运行命令的软件包,则可以使用–s选项模拟升级。

sudo apt-get –s upgrade

sudo apt-get –s升级

This is really useful if you aren’t sure if upgrading one package will mess up other programs, which happens occasionally with things like PHP and mail server libraries.

如果您不确定升级一个程序包是否会使其他程序弄乱,这真的很有用,因为偶尔会发生这种情况,例如PHP和邮件服务器库。

清洁用品 (Cleaning)

When you download packages, Ubuntu caches them in case it needs to refer to them further. You can delete this cache and get back some hard drive space with the following command:

下载软件包时,Ubuntu会缓存它们,以防需要进一步引用它们。 您可以使用以下命令删除此缓存并获取一些硬盘空间:

sudo apt-get clean

须藤apt-get clean

If you want to get rid of your cache, but save the newest versions of what packages you have, then use this instead:

如果要摆脱缓存,但保存所拥有软件包的最新版本,请改用以下方法:

sudo apt-get autoclean

须藤apt-get autoclean

This will get rid of the older versions which are pretty much useless, but still leave you with a cache.

这将摆脱几乎没有用的旧版本,但仍然会留下缓存。

检查安装了什么 (Checking What’s Installed)

You can see a list of all your installed packages with dpkg.

您可以使用dpkg查看所有已安装软件包的列表。

sudo dpkg –list

sudo dpkg –列表

You can also use less to scroll through this list.

您也可以使用更少的次数滚动浏览此列表。

sudo dpkg –list | less

sudo dpkg –列表| 减

You can also search through the list with the grep command.

您也可以使用grep命令搜索列表。

dpkg –list | grep [search term]

dpkg –列表| grep [搜索词]

If something is installed, you’ll see a package name and a description.

如果安装了某些软件,您将看到软件包名称和说明。

You can also search through a more compact method:

您还可以搜索更紧凑的方法:

dpkg –l ‘search term’

dpkg –l'搜索词'

That option is a lowercase letter L, and your search term must be inside single quotes. You can use wildcard characters to search better as well.

该选项是小写字母L,并且您的搜索词必须在单引号内。 您也可以使用通配符来更好地进行搜索。

如何从命令行使用Apt-Get在Ubuntu中安装程序

复活节彩蛋 (Easter Egg)

APT has an interesting easter egg.

APT有一个有趣的复活节彩蛋。

sudo apt-get moo

须藤apt-get moo

Enjoy your super cow powers!

享受您的超级牛!



Being able to manage packages and installed software via command-line can save you some time. Ubuntu’s Software Updater is often laggy on my system and it can really be a pain to have to add software repos and install packages through the Software Center, especially if you know the package names already. It’s also great for managing your system remotely via SSH. You don’t need to have a GUI running at all or deal with VNC.

能够通过命令行管理软件包和已安装的软件可以为您节省一些时间。 Ubuntu的软件更新程序经常在我的系统上运行缓慢,必须通过Software Center添加软件存储库并安装软件包确实很痛苦,特别是如果您已经知道软件包名称的话。 通过SSH远程管理系统也非常有用。 您根本不需要运行GUI或处理VNC。

There are a lot of things to learn when getting comfortable with the command-line, so you may want to check out The Beginner’s Guide to Nano, the Linux Command-Line Text Editor. There’s more to come!

熟悉命令行后,有很多东西要学习,因此您可能需要查看Linux命令行文本编辑器Nano入门指南 。 还有更多!

翻译自: https://www.howtogeek.com/63997/how-to-install-programs-in-ubuntu-in-the-command-line/