如何在Linux上使用Git安装软件

Code on a laptop screen
MchlSkhrv/Shutterstock MchlSkhrv / Shutterstock

Have you been told to “clone the repo and build it,” and don’t know what to do next? We’ll show you how to get that program on GitHub running on Linux, even if you’re a beginner.

您是否被告知要“克隆存储库并构建它”,并且不知道下一步该怎么做? 即使您是初学者,我们也会向您展示如何在Linux上的GitHub上获取该程序。

The instructions that make up a computer program are written, edited, and saved in text files. A program called a compiler then processes these files. This produces the executable version of the program. The text files of instructions are called the source code. The version of the program that can actually run on a computer is called the binary or the executable.

组成计算机程序的指令被编写,编辑并保存在文本文件中。 然后,称为编译器的程序将处理这些文件。 这将生成程序的可执行版本 。 指令的文本文件称为源代码。 实际上可以在计算机上运行的程序的版本称为二进制文件或可执行文件。

That’s a simplified version of events, but it paints a correct—if generalized—picture. In practice, you’ll find all sorts of variations on that model. Sometimes, other programs generate the text files. Other times, the source code runs inside an interpreter and doesn’t need to be compiled, and so on.

那是事件的简化版本,但是它描绘了正确的(如果是概括的)画面。 在实践中,您会在该模型上找到各种变化。 有时,其他程序会生成文本文件。 有时,源代码在解释器中运行,不需要编译,依此类推。

However, the one universal truth across all software projects is this: the source code files are the crown jewels, and they need to be looked after just as carefully.

但是,所有软件项目的一个普遍真理是:源代码文件是头等大事,需要同样谨慎地对待它们。

版本控制程序 (Version Control Programs)

All of the source code files within a project are called the codebase. Large projects often have many developers working on the codebase. Every code change must be tracked and identifiable. If required, the changes must be reversible. If different developers make changes to the same source code file, their edits must be merged.

项目中的所有源代码文件都称为代码库。 大型项目通常有许多开发人员在代码库上工作。 每个代码更改都必须得到跟踪和识别。 如果需要,更改必须是可逆的。 如果不同的开发人员对同一源代码文件进行了更改,则必须合并他们的编辑。

It’s not surprising, then, that software programs called version control systems exist to make the management of changes to the codebase easier. Version control systems hold all previous versions of each file in the codebase, and every change is recorded, commented on, and tracked.

因此,毫不奇怪的是,存在称为版本控制系统的软件程序,可以简化对代码库的更改的管理。 版本控制系统在代码库中保存每个文件的所有先前版本,并且记录,注释和跟踪每个更改。

一个叫做Git的小东西 (A Little Thing Called Git)

Linus Torvalds, the creator of the Linux kernel, developed a version control program called Git to administer the Linux kernel codebase. It’s now the world’s most widely adopted version control software. There are millions of people using it—literally.

Linux内核的创建者 Linus Torvalds开发了一个名为Git的版本控制程序来管理Linux内核代码库。 它现在是世界上使用最广泛的版本控制软件。 从字面上看,有数百万人在使用它。

With Git, a project’s codebase is stored in repositories. In addition to the local repositories that sit on developer’s computers and, perhaps, on a central server on the network, it’s a good practice to have an off-site, or remote, repository.

使用Git,项目的代码库存储在存储库中 。 除了位于开发人员的计算机上的本地存储库,或者也许位于网络中央服务器上的本地存储库,还有一个不错的做法是拥有一个异地或远程存储库。

And that’s where GitHub comes in.

这就是GitHub出现的地方。

的GitHub (GitHub)

GitHub was created as a result of git‘s success. The founders saw the emerging need for securely hosted remote git repositories. They launched a business providing a cloud platform to allow development teams to host remote repositories. As of April 2019, GitHub hosts over 100 million repositories.

GitHubgit成功的结果。 创始人看到了对安全托管的远程git存储库的新兴需求。 他们启动了提供云平台的业务,以允许开发团队托管远程存储库。 截至2019年4月,GitHub托管了超过1亿个存储库。

If an application is an open-source project, the chances are very high that it will be hosted on GitHub. There are other repository platforms available, such as BitBucket and GitLab, but GitHub has the lion’s share of open source repositories.

如果应用程序是一个开源项目,那么将其托管在GitHub上的可能性很高。 还有其他可用的存储库平台,例如BitBucketGitLab ,但是GitHub在开源存储库中占有最大份额。

仓库的剖析 (Anatomy of a Repository)

A GitHub repository is comprised of folders containing files such as the all-important source code files. Usually, there are many other types of files in the repository. There might be documentation files, man pages, software license files, build instructions and shell script files. There are no rules regarding what a repository should or must contain, but there are conventions.

GitHub存储库由包含所有重要源代码文件之类的文件的文件夹组成。 通常,存储库中还有许多其他类型的文件。 可能有文档文件,手册页,软件许可证文件,构建说明和Shell脚本文件。 对于存储库应包含或必须包含的内容,没有任何规则,但是有约定。

If you know your way around one kitchen, you can navigate any kitchen. It’s the same with repositories. Once you understand the conventions, you know where to go to find what you need.

如果您知道围绕一个厨房的方式,则可以导航任何一个厨房。 存储库也是如此。 了解约定后,您便知道去哪里可以找到所需的内容。

So, how do you get a copy of the repository on your computer, and how do you build the program into a binary executable?

因此,如何在计算机上获取存储库的副本,以及如何将程序构建为二进制可执行文件?

自述文件 (The readme File)

It’s traditional to include a readme file in a repository. It might be called readme, Readme, or README. It might have an extension of “.md” or no extension at all.

传统上是在存储库中包含自述文件。 它可能称为自述文件,自述文件或自述文件。 它的扩展名可能是“ .md”或根本没有扩展名。

Let’s have a look at the GitHub repository for the Atom editor. You see a long list of folders and files. Scroll down, and you see the contents of the README.md file.

让我们看一下Atom编辑器的GitHub 存储库 。 您会看到一长串的文件夹和文件。 向下滚动,您会看到README.md文件的内容。

GitHub automatically puts the contents of the readme file on the front page of the repository. If the readme file has a “.md” extension, it will contain Markdown markup language. This allows the developers to use style elements, such as fonts, bullet points, and images.

GitHub自动将自述文件的内容放在存储库的首页上。 如果自述文件的扩展名为“ .md”,则它将包含Markdown标记语言。 这使开发人员可以使用样式元素,例如字体,项目符号和图像。

Section of the readme.md file for the atom editor on github.

Typically, a readme file has sections that tell you what the project is about, what the type license is, who maintains the project, how to get involved, and how to build and run the application.

通常,自述文件的各个部分将告诉您项目的内容,类型许可证的类型,维护项目的人员,如何参与以及如何构建和运行应用程序。

If it doesn’t list the actual build instructions, it will tell you where to find this information. Other information useful to building the application, such as the build tools required and other dependencies, might be listed here or a link might take you to that information.

如果未列出实际的构建说明,它将告诉您在哪里可以找到此信息。 其他可能对构建应用程序有用的信息,例如所需的构建工具和其他依赖项,可能会在此处列出,或者链接可能会将您带到该信息。

盒子仓库 (The boxes Repository)

Our mission is to clone the boxes repository, and then build the boxes application.

我们的任务是克隆box的存储库 ,然后构建boxes应用程序。

The repository follows the same layout the Atom one did. There’s a list of folders and files and below that is the contents of the readme file. It follows the standard layout for a repository, but it’s a smaller project, so there are fewer folders and files.

该存储库遵循与Atom相同的布局。 有一个文件夹和文件列表,下面是自述文件的内容。 它遵循存储库的标准布局,但是它是一个较小的项目,因此文件夹和文件更少。

The readme file is briefer too. It has a section called “Development.” In that section is a link entitled “building from source.” If we follow that link, we should find the information we need.

自述文件也更简短。 它有一个称为“开发”的部分。 在该部分中,有一个标题为“从源头构建”的链接。 如果我们点击该链接, 我们应该找到所需的信息

Link to the build instructions for the boxes application.

There’s usually some lightweight sleuthing necessary to navigate the repository and find the information you want, but it’s not difficult. Read everything on the repository page carefully. Sometimes, the information is there but might not be prominently displayed.

通常,导航存储库并找到所需信息需要一些轻量级的侦查,但这并不困难。 仔细阅读存储库页面上的所有内容。 有时,信息在那里,但可能不会突出显示。

依赖关系 (The Dependencies)

The “Building from Source” page has a section called “Building on Linux,” and that’s just what we need. It says we must have a C compiler, Bison, and Flex installed.

“从源代码构建”页面上有一个名为“在Linux上构建”的部分,这正是我们所需要的。 它说我们必须安装C编译器BisonFlex

Required tool set for building the boxes application

The build instructions say to issue the make command, so we’ll also need make.

生成指令说要发出make命令,因此我们也需要make

The tools required to build this application are a C compiler, Bison, Flex, make, and Git (to clone the repository to your computer).

构建该应用程序所需的工具是C编译器,Bison,Flex, make和Git(用于将存储库克隆到您的计算机)。

This article was researched on computers running the Ubuntu, Fedora, and Manjaro Linux distributions. None of the distribution had all of these tools installed—something had to be installed on each of them.

本文是在运行Ubuntu,Fedora和Manjaro Linux发行版的计算机上进行研​​究的。 所有发行版都没有安装所有这些工具,而每个工具都必须安装一些工具。

安装工具箱 (Installing the Tool Set)

Ubuntu had to have Git, Flex, Bison, and make installed. Here are the commands:

Ubuntu必须安装Git,Flex,Bison和make 。 以下是命令:

sudo apt-get install git
"sudo apt-get install git" in a terminal window.
sudo apt-get install flex
"sudo apt-get install flex" in a terminal window.
sudo apt-get install bison
"sudo apt-get install bison" in a terminal window.
sudo apt-get install make
"sudo apt-get install make" in a terminal window.

Fedora had to have Flex, Bison, and make installed. Here are the commands:

Fedora必须安装Flex,Bison并make安装。 以下是命令:

sudo dnf install flex
"sudo dnf install flex" in a terminal window
sudo dnf install bison
"sudo dnf install bison" in a terminal window.
sudo dnf install make
"sudo dnf install make" in a terminal window.

Manjaro had to have the GCC compiler, Flex, and Bison installed. Here are the commands:

Manjaro必须安装GCC编译器,Flex和Bison。 以下是命令:

sudo pacman -Syu gcc
"sudo pacman -Syu gcc" in a terminal window.
sudo pacman -Syu flex
"sudo pacman -Syu flex" in a terminal window.
sudo pacman -Syu bison
"sudo pacman -Syu bison" in a terminal window.

克隆存储库 (Cloning the Repository)

Each GitHub repository has a specific web address used with Git to clone the repository to your computer. On the main page of the boxes repository, there’s a green button labeled “Clone or download.”

每个GitHub存储库都有一个与Git一起使用的特定网址,以将该存储库克隆到您的计算机。 在box资源库的主页上,有一个标有“克隆或下载”的绿色按钮。

The "Clone or Download" button in GitHub.

Click the button to see the web address. This is the address we must pass to the git command when we clone the repository.

点击按钮查看网址。 这是克隆存储库时必须传递给git命令的地址。

Change into the directory that we want to have the repository cloned into, and then use this command. If your terminal window supports it, you can copy and paste the web address into the command. Press Ctrl+Shift+V to paste into a GNOME terminal window.

切换到我们想要将存储库克隆到的目录,然后使用此命令。 如果您的终端窗口支持它,则可以将网址复制并粘贴到命令中。 按Ctrl + Shift + V粘贴到GNOME终端窗口。


   
   
"git clone https://github.com/ascii-boxes/boxes.git" in a terminal window.

Git clones the remote repository and creates a local one on your computer. It tells us it’s cloning into a directory called “boxes.”

Git克隆远程存储库并在您的计算机上创建本地存储库。 它告诉我们它正在克隆到一个名为“ boxes”的目录中。

Cloned repository in a terminal window.

The boxes directory is created within the directory from which you issued the git command. If we switch to the boxes directory and look at the contents, we see the same list of files and folders we saw on the GitHub page.

在发出git命令的目录中创建了git目录。 如果切换到boxs目录并查看其内容,则会看到与GitHub页面上相同的文件和文件夹列表。


   
   
The boxes repository contents in a terminal window.

Great! We’ve successfully cloned the source code and other files to our computer. Now, we need to build the application.

大! 我们已成功将源代码和其他文件克隆到我们的计算机中。 现在,我们需要构建应用程序。

构建应用程序 (Building the Application)

To build the application, we must follow the instructions on the GitHub repository. Sometimes, we’ll run a particular shell file, and others we’ll run  make.  The build instructions we’re following told us to run make.

要构建应用程序,我们必须遵循GitHub存储库上的指示。 有时,我们将运行特定的外壳文件,而其他人将运行make 。 我们遵循的构建说明告诉我们运行make

The make utility reads and performs a set of instructions from a makefile. These instructions tell make how to compile the program and link it together. make passes the instructions to the compiler and other build tools.

make实用程序从makefile读取并执行一组指令。 这些指令告诉make如何编译程序和链接在一起的。 make将指令传递给编译器和其他构建工具。

The command we’re told to use will call make twice. The first call to make builds the application, and the second runs a suite of tests.

我们被告知要使用的命令将调用make两次。 第一个调用make构建应用程序,第二个调用运行一系列测试。

The command the build instructions told us to use is:

构建说明告诉我们使用的命令是:

make && make test
"make && make test" in a terminal window.

Many lines of output scroll by rapidly in the terminal window. In a minute or so, you’ll be returned to the command prompt.

在终端窗口中,许多行的输出快速滚动。 大约一分钟后,您将返回到命令提示符。

部署盒子应用程序 (Deploying the boxes Application)

The application has been built, and we have an executable binary. We must now copy the binary to the /usr/bin/ directory. This allows the shell to find it when we try to use it.

该应用程序已构建,并且我们有一个可执行的二进制文件。 现在,我们必须将二进制文件复制到/ usr / bin /目录。 这样,当我们尝试使用外壳程序时,它就可以找到它。

For some applications, this might be all you have to do. In other cases, you might need to copy additional files, such as man pages and config files, into locations in the filesystem. The latter is what we have to do with our new application because it was in the build instructions.

对于某些应用程序,这可能就是您要做的。 在其他情况下,可能需要将其他文件(例如手册页和配置文件)复制到文件系统中的位置。 后者是我们与新应用程序必须要做的事情,因为它在构建说明中。

File copy commands from GitHub.

Use sudo to run these commands. The first command copies a man page into the man1 directory:

使用sudo运行这些命令。 第一个命令将手册页复制到man1目录中:

sudo cp doc/boxes.1 /usr/share/man/man1
"sudo cp doc/boxes.1 /usr/share/man/man1" in a terminal window.

Next, copy the global config file to a directory in /usr/share/:

接下来,将全局配置文件复制到/ usr / share /中的目录:

sudo cp boxes-config /usr/share/boxes
"sudo cp boxes-config /usr/share/boxes" in a terminal window.

Finally, copy the binary to /usr/bin:

最后,将二进制文件复制到/ usr / bin:

sudo cp src/boxes /usr/bin
"sudo cp src/boxes /usr/bin" in a terminal window.

测试盒子应用 (Testing the boxes Application)

Let’s see if it all works! Try to open the man page for the boxes command.

让我们看看这一切是否有效! 尝试打开boxes命令的手册页。

man boxes
"man boxes" in a terminal window.

That’s encouraging! You see a man page telling you how to use the boxes command.

令人鼓舞! 您会看到一个手册页,告诉您如何使用boxes命令。

"boxes man" page in a terminal window.

Press “Q” to leave the man system and try to use the boxes command.

按“ Q”离开人工系统并尝试使用boxes命令。

echo How-To Geek | boxes
"boxes How-To Geek" in a terminal window.

And we get the response:

我们得到了响应:

boxes output in a terminal window.

This might seem slightly underwhelming considering all the effort you’ve gone to, but the point of this exercise was to walk you through pulling back a repository from GitHub and building the application.

考虑到您已付出的所有努力,这似乎有些不足,但是此练习的重点是引导您完成从GitHub撤回存储库并构建应用程序的过程。

The boxes command allows you to wrap text that’s piped to it in a wide variety of frames. Some of them could be used as comments in source code files. The format above would work as a comment in a C source code file, for example. Others are purely decorative. The -d (design) option allows you to choose the style of the frame.

boxes命令允许您将通过管道传递给它的文本包装在各种框架中。 其中一些可用作源代码文件中的注释。 例如,以上格式将用作C源代码文件中的注释。 其他人纯粹是装饰性的。 -d (设计)选项允许您选择框架的样式。

echo How-To Geek | boxes -d whirly
echo How-To Geek | boxes -d c-cmt2
"echo How-To Geek | boxes -d whirly" in a terminal window.

There’s a long list of designs from which you can choose. To see them all, use this command:

有很多设计可供选择。 要查看所有内容,请使用以下命令:

boxes -l | less

构建完成 (Build Complete)

The steps to build from source are usually straightforward:

从源代码构建的步骤通常很简单:

  • Review the build instructions on the repository.

    查看存储库上的构建说明。
  • Check you have the required tools installed and install any that are missing.

    检查您是否已安装必需的工具并安装所有缺少的工具。
  • Clone the repository to your computer.

    将存储库克隆到您的计算机。
  • Follow the build instructions, which are often as simple as typing make.

    遵循构建说明,该说明通常与键入make一样简单。

  • Copy the file(s) to the required locations.

    将文件复制到所需位置。

If there are steps in the build instructions that are unclear, see if the project has a forum or community you can send a question to. If the application has a website, they might have a “Contact Us” page. The developer who maintains the boxes project has his email on the “About” page of the boxes website. That’s a generous gesture on his part, and typical of the wider open source community.

如果构建说明中有不清楚的步骤,请查看项目中是否有可以向其发送问题的论坛或社区。 如果应用程序具有网站,则它们可能具有“联系我们”页面。 维护box项目的开发人员将其电子邮件发送到box网站的“关于”页面 。 这是他慷慨的举动,也是更广泛的开源社区的典型代表。

翻译自: https://www.howtogeek.com/428988/how-to-install-software-using-git-on-linux/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值